﻿/**********************************************
 * 功能名称：成本归集点设置
编写人：  杨莹
建立时间： 杨 2008年11月10日
 *修改时间： 杨莹 2008年11月20日
说明：
 * 列表作业中心目录树。对每一个作业中心的资源属性进行设置。
 * 当选择一个checkedBOX的时候，即表示当前作业中心是该项资源的成本归集点。
 * 本页面提供“重置”功能。可根据作业中心的打勾情况，重建当前会计期间的成本归集点金额明细表。
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

using System.Data.Linq;
using System.Collections.Generic;


namespace Hisun.View.Resource
{
    public partial class ResouceSetup : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CreateTree();
            }
        }


        #region 生成作业中心目录数
        private void MakeMenu(TABC_JS_CostCenter[] menuSource)
        {
            foreach (var node in menuSource)
            {
                // 创建父节点
                if (node.上级作业中心ID == 0)
                {
                    TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                    // newNode.NavigateUrl = node.页面文件;
                    this.TreeView_CenterTree.Nodes.Add(newNode);
                    GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
                }
            }
        }


        /// <summary>
        /// 产生子节点
        /// </summary>
        /// <param name="childNodes"></param>
        /// <param name="fatherNode"></param>
        /// <param name="menuSource"></param>
        private void GenerateSubMenu(TABC_JS_CostCenter[] childNodes, TreeNode fatherNode, TABC_JS_CostCenter[] menuSource)
        {
            foreach (TABC_JS_CostCenter node in childNodes)
            {
                TreeNode newNode = new TreeNode(node.作业中心名称, node.作业中心ID.ToString());
                // newNode.NavigateUrl = node.页面文件;

                fatherNode.ChildNodes.Add(newNode);

                GenerateSubMenu(GetSubNodeSource(node, menuSource), newNode, menuSource);
            }
        }
        /// <summary>
        /// 产生子节点的数据源
        /// </summary>
        /// <param name="father"></param>
        /// <param name="menuSource"></param>
        /// <returns></returns>
        private TABC_JS_CostCenter[] GetSubNodeSource(TABC_JS_CostCenter father, TABC_JS_CostCenter[] menuSource)
        {
            List<TABC_JS_CostCenter> subNodesSource = new List<TABC_JS_CostCenter>();
            foreach (TABC_JS_CostCenter node in menuSource)
            {
                //if (father.是否末级 == 'T')
                //{
                //    break;
                //}
                //else
                //{
                if (node.上级作业中心ID == father.作业中心ID)
                {
                    subNodesSource.Add(node);
                }
                //}
            }
            return subNodesSource.ToArray();
        }
        #endregion

        private void CreateTree()
        {
            var MyCenterTree = (from a in db.TABC_JS_CostCenter select a).ToArray();
            this.TreeView_CenterTree.Nodes.Clear();
            MakeMenu(MyCenterTree);


        }

        protected void TreeView_CenterTree_SelectedNodeChanged(object sender, EventArgs e)
        {

            int MyCenterId = Convert.ToInt32(TreeView_CenterTree.SelectedNode.Value);
            string MyCenterName = TreeView_CenterTree.SelectedNode.Text;

            BindMyData(MyCenterId, MyCenterName);
        }

        private void BindMyData(int MyCenterId, string MyCenterName)
        {
            var MyQueryResouceSetupRowCount = (from a in db.TABC_JS_ResourceSetup where a.作业中心ID == MyCenterId select a).Count();

            if (MyQueryResouceSetupRowCount == 0)
            {

                Label_Id.Text = MyCenterId.ToString();
                Label_Name.Text = MyCenterName;
                CheckBox_RZ.Checked = false;
                CheckBox_SB.Checked = false;
                CheckBox_ZZFY.Checked = false;
                CheckBox_SF.Checked = false;
                CheckBox_DF.Checked = false;
                CheckBox_ZQF.Checked = false;
                CheckBox_XZSB.Checked = false;
                CheckBox_CFZJ.Checked = false;
                CheckBox_SL.Checked = false;

            }
            else
            {
                var MyQueryResouceSetupRow = (from a in db.TABC_JS_ResourceSetup where a.作业中心ID == MyCenterId select a).Single();
               
                Label_Id.Text = MyQueryResouceSetupRow.作业中心ID.ToString();
                Label_Name.Text = MyCenterName;
                CheckBox_RZ.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.人工);
                CheckBox_SB.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.折旧);
                CheckBox_ZZFY.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.制造费用);
                CheckBox_SF.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.水);
                CheckBox_DF.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.电);
                CheckBox_ZQF.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.蒸汽);
                CheckBox_XZSB.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.闲置);
                CheckBox_CFZJ.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.厂房);
                CheckBox_SL.Checked = Convert.ToBoolean(MyQueryResouceSetupRow.数量);
            }
         
        }









        //修改当前作业中心的资源归集属性。
        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            int MyQueryId = Convert.ToInt32(Label_Id.Text);
            var MyQueryResouceSetupRowCount = (from a in db.TABC_JS_ResourceSetup where a.作业中心ID == MyQueryId select a).Count();


            if (MyQueryResouceSetupRowCount == 0)
            {

                TABC_JS_ResourceSetup MyNewRow = new TABC_JS_ResourceSetup();

                MyNewRow.作业中心ID = MyQueryId;
                MyNewRow.人工 = CheckBox_RZ.Checked;
                MyNewRow.折旧 = CheckBox_SB.Checked;
                MyNewRow.制造费用 = CheckBox_ZZFY.Checked;
                MyNewRow.水 = CheckBox_SF.Checked;
                MyNewRow.电 = CheckBox_DF.Checked;
                MyNewRow.蒸汽 = CheckBox_ZQF.Checked;
                MyNewRow.闲置 = CheckBox_XZSB.Checked;
                MyNewRow.厂房 = CheckBox_CFZJ.Checked;
                MyNewRow.数量 = CheckBox_SL.Checked;
                db.TABC_JS_ResourceSetup.InsertOnSubmit(MyNewRow);
            }
            else
            {
                var MyEditRow = (from a in db.TABC_JS_ResourceSetup where a.作业中心ID == MyQueryId select a).Single();

                MyEditRow.人工 = CheckBox_RZ.Checked;
                MyEditRow.折旧 = CheckBox_SB.Checked;
                MyEditRow.制造费用 = CheckBox_ZZFY.Checked;
                MyEditRow.水 = CheckBox_SF.Checked;
                MyEditRow.电 = CheckBox_DF.Checked;
                MyEditRow.蒸汽 = CheckBox_ZQF.Checked;
                MyEditRow.闲置 = CheckBox_XZSB.Checked;
                MyEditRow.厂房 = CheckBox_CFZJ.Checked;
                MyEditRow.数量 = CheckBox_SL.Checked;
            }



            try
            {
                db.SubmitChanges();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改成功'); </script>");


            }
            catch
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('修改失败'); </script>");
            }




        }


        //对数据库里的资源表进行初始化，把当月所有需要归集指定资源的记录建立表结构 ，并初始化金额数据为零。
        protected void Button_Reset_Command(object sender, CommandEventArgs e)
        {
            string CurrenctAccountPeriod = Application["accountPeriod"].ToString();
            var MyOldCenterItems = from b in db.TABC_JC_CostCenterItems where b.会计期间ID == CurrenctAccountPeriod select b;
            db.TABC_JC_CostCenterItems.DeleteAllOnSubmit(MyOldCenterItems);
            db.SubmitChanges();

            var MyResouceSetups = (from a in db.TABC_JS_ResourceSetup select a).Distinct();

            //事务处理代码阶段
            if (db.Connection != null) db.Connection.Open();
            System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction();
            db.Transaction = tran;


            try
            {
                foreach (var MyResourcesetup in MyResouceSetups)
                {
                    int Myid = MyResourcesetup.作业中心ID;
                    if (MyResourcesetup.人工 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "RG";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }


                    if (MyResourcesetup.折旧 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "SB";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }

                    if (MyResourcesetup.厂房 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "CF";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }

                    if (MyResourcesetup.制造费用 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "ZZ";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }

                    if (MyResourcesetup.水 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "SF";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }

                    if (MyResourcesetup.电 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "DF";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }

                    if (MyResourcesetup.蒸汽 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "ZQ";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }

                    if (MyResourcesetup.闲置 == true)
                    {
                        TABC_JC_CostCenterItems MyRG = new TABC_JC_CostCenterItems();
                        MyRG.作业中心ID = Myid;
                        MyRG.会计期间ID = CurrenctAccountPeriod;
                        MyRG.成本细项ID = "XZ";
                        MyRG.公司ID = "hisun";
                        MyRG.金额 = 0;
                        db.TABC_JC_CostCenterItems.InsertOnSubmit(MyRG);
                        db.SubmitChanges();

                    }





                }

                tran.Commit();
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('重置成功'); </script>");

            }
            catch
            {

                tran.Rollback();
            }




        }
    }
}
